package com.ctech.wsgen.writer;

import java.io.File;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Element;

import com.ctech.wsgen.model.Item;
import com.ctech.wsgen.model.Manufacture;
import com.ctech.wsgen.model.Site;

public class ManufactureDom4JHtmlWriter extends AbstractDom4JHtmlWriter  {

	private Logger log = Logger.getLogger(ManufactureDom4JHtmlWriter.class.getName());
	
	Manufacture manufacture;
	Site site;
	
	public ManufactureDom4JHtmlWriter(Manufacture manufacture, Site site) throws Exception {
		if(manufacture != null && manufacture.getItems() != null) {
			this.manufacture = manufacture;
			this.site = site;
			File file = new File(site.getSiteDir().getAbsolutePath().concat(File.separator + manufacture.getFileName()));
			if(!file.exists()) file.createNewFile();
			createHtml(file, this.site.getJavaScriptBlock());
			if(log.isInfoEnabled()) log.info("Created " + this.manufacture.getFileName() + " with " + this.manufacture.getItems().size() + " items");
		}else {
			log.error("No items for Manufacture: " + manufacture.getVendorName());
		}
	}

	@Override
	public String getContentHeaderTitle() {
		return this.manufacture.getVendorName();
	}

	@Override
	public List<Item> getItems() {
		return this.manufacture.getItems();
	}

	@Override
	public String getMetaContentName() {
		return this.manufacture.getVendorName();
	}

	@Override
	public String getTitleName() {
		return this.manufacture.getVendorName();
	}

	@Override
	public void createContentFooter(Element footerTable) {
		/*
		 *  footer for Manufactures pages 
		<table>
			<!-- START: Content Footer Area -->
			<tr>
				<td class="ContentFooter">
					For more information please visit the manufactures website at 
					<a href="http://www.activesafety.com" target=_blank>http://www.activesafety.com</a>
				</td>
			</tr>
			<!-- END: Content Footer Area -->
		</table>
		 */
		if(this.manufacture.getWebsiteURL() != null) {
			footerTable.addText("For more information please visit the manufactures website at ");
			footerTable.addElement("a")
				.addAttribute("href", this.manufacture.getWebsiteURL())
				.addAttribute("target", "_blank")
				.addText(this.manufacture.getWebsiteURL());
		}
	}

	@Override
	public String getEmailAddress() {
		return this.site.getStr_email();
	}

	@Override
	public void createContentHeaderTitle(Element divHeader) {
		/*
		<table class="HeaderTitle">
			<tbody>
				<tr>
					<td valign="middle">
						<img src="images/Advanced_Sourcing_logo.jpg" style="float: left;" title="Advanced Sourcing">
						<img src="images/Advanced_Sourcing_logo.jpg" style="float: right;" title="Advanced Sourcing">						
						Advanced Sourcing
					</td>
				</tr>
			</tbody>
		</table>
	*/
	Element contentHeaderTD = divHeader.addElement("table").addAttribute("class", "HeaderTitle")
		.addElement("tbody").addElement("tr").addElement("td").addAttribute("valign", "middle").addText(getContentHeaderTitle());
	contentHeaderTD.addElement("img").addAttribute("src", "images//" + manufacture.getLogoFileName()).addAttribute("style", "float: left;")
		.addAttribute("title", getContentHeaderTitle());
	contentHeaderTD.addElement("img").addAttribute("src", "images//" + manufacture.getLogoFileName()).addAttribute("style", "float: right;")
		.addAttribute("title", getContentHeaderTitle());	
	}

	@Override
	public String getWebsiteURL() {
		return this.site.getStr_websiteUrl();
	}

	
}
